package com.zzy.util;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipExample {
    public static void main(String[] args) {

//        List<String> fileList = getAllFile("E:/aaa");
//        System.out.println(fileList);

//        List<String> fileList  = new ArrayList<>();
//        getAllFile2("E:/aaa",fileList);
//        System.out.println(fileList);

        List<String> filesToZip = Arrays.asList("E:/aaa/soft01/","E:/aaa/soft02/");
        String zipFileName = "E:/aaa/zipExample.zip";
        zipFileNoDir(filesToZip,zipFileName);

//        List<String> filesToZip = Arrays.asList("E:/aaa/soft01/aaa.txt","E:/aaa/soft01/bbb.txt");
//        String zipFileName = "E:/zipExample2.zip";
//        zipFileNoDir(filesToZip,zipFileName);

    }

    /** 获取目录下的所有文件 */
    public static List<String> getAllFile(String dir){
        List<String> fileList = new ArrayList<>();
       try {
           Path startPath = Paths.get(dir);
           Files.walk(startPath)
                   .filter(Files::isRegularFile)
                   .forEach(e->{
                       fileList.add( e.toAbsolutePath().toString() );
                   });
       }catch (Exception e){
           e.printStackTrace();
       }
       return fileList;
    }

    /** 获取目录下的所有文件 */
    public static void getAllFile2(String dir,List<String> fileSrc) {
        File fileTmp = new File(dir);
        if (fileTmp.isDirectory()) {
            File[] files = fileTmp.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        getAllFile2(file.getAbsolutePath(),fileSrc); // 递归处理子目录
                    } else {
                        fileSrc.add( file.getAbsolutePath());
                    }
                }
            }
        }
    }

    /**
     * 压缩后的文件不带目录
     */
    public static void zipFileNoDir(List<String> filesToZip ,String zipFileName){
        try (ZipOutputStream zipOut = new ZipOutputStream(
                new FileOutputStream(zipFileName))) {
            for (String filePath : filesToZip) {
                File file = new File(filePath);
                if (file.exists()) {
                    addToZip(file, file.getName(), zipOut);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void addToZip(File file, String entryName, ZipOutputStream zipOut) throws IOException {
        try {
        if (file.isDirectory()) {
            // 处理目录
            zipOut.putNextEntry(new ZipEntry(entryName + "/"));
            zipOut.closeEntry();
            File[] children = file.listFiles();
            if (children != null) {
                for (File child : children) {
                    addToZip(child, entryName + "/" + child.getName(), zipOut);
                }
            }
        } else {
            // 处理文件
            try (FileInputStream fis = new FileInputStream(file)) {
                ZipEntry zipEntry = new ZipEntry(entryName);
                zipOut.putNextEntry(zipEntry);
                byte[] bytes = new byte[1024];
                int length;
                while ((length = fis.read(bytes)) >= 0) {
                    zipOut.write(bytes, 0, length);
                }
                zipOut.closeEntry();
            }
        }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
